﻿using System.Web.Mvc;
using Nop.Core;
using Nop.Core.Caching; 
using Nop.Services.Configuration;
using Nop.Services.Localization;
using Nop.Services.Media;
using Nop.Services.Stores;
using Nop.Web.Framework.Controllers;
using Nop.Plugin.Picture.CDN.Models;

namespace Nop.Plugin.Picture.CDN.Controllers
{
    [AdminAuthorize]
    public class NopCDNController : BasePluginController
    {
        private readonly CDNPluginSettings _cdnPluginSettings;
        private readonly ISettingService _settingService;
        // We need to use this ReponseCache to verify current licensekey if is validate
        // for ourself disabled licensekey.
        //protected ObjectCache ResponseCache
        //{
        //    get
        //    {
        //        return MemoryCache.Default;
        //    }
        //}
        public NopCDNController(CDNPluginSettings cdnPluginSettings, ISettingService settingService)
        {
            this._cdnPluginSettings = cdnPluginSettings;
            this._settingService = settingService;
        }
        public ActionResult Configure()
        {
            ActionResult result;
            if (string.IsNullOrEmpty(this._cdnPluginSettings.LicenseKey))
            {
                result = base.RedirectToRoute("Nop.Plugin.Picture.CDN.LicenseKey");
            }
            else
            {
                result = base.View("~/Plugins/Nop.Plugin.Picture.CDN/Views/NopCDN/Configure.cshtml", new ConfigureModel
                {
                    DomainNames = this._cdnPluginSettings.DomainNames,
                    Enable = this._cdnPluginSettings.Enable
                });
            }
            return result;
        }
        [FormValueRequired(new string[] { "configure" }), HttpPost]
        public ActionResult Configure(ConfigureModel model)
        {
            ActionResult result;
            if (!base.ModelState.IsValid)
            {
                result = this.Configure();
            }
            else
            {
                this._cdnPluginSettings.DomainNames = model.DomainNames;
                this._cdnPluginSettings.Enable = model.Enable;
                this._settingService.SaveSetting<CDNPluginSettings>(this._cdnPluginSettings);
                result = this.Configure();
            }
            return result;
        }
        public ActionResult LicenseKey()
        {
            LicenseKeyModel model = new LicenseKeyModel();
            model.LicKey = this._cdnPluginSettings.LicenseKey;
            return base.View("~/Plugins/Nop.Plugin.Picture.CDN/Views/NopCDN/LicenseKey.cshtml");
        }
        [FormValueRequired(new string[] { "save" }), HttpPost]
        public ActionResult LicenseKey(LicenseKeyModel model)
        {
            this._cdnPluginSettings.LicenseKey = model.LicKey;
            this._settingService.SaveSetting<CDNPluginSettings>(this._cdnPluginSettings);
            return base.RedirectToRoute("Nop.Plugin.Picture.CDN.Configure");
        }
        public ActionResult DemoOver()
        {
            return base.Content("Your Demo has been over.. ");
        }
        public ActionResult AccessDenied()
        {
            return base.Content("Unauthorized License.. Please Register..");
        }
        public ActionResult LicenseKeyMissMatch()
        {
            return base.Content("Please check your license key... Your License key is missmatched... ");
        }
    }
}